package com.example.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hospital.pojo.*;
import com.example.hospital.service.IBasicService;
import com.example.hospital.service.IPatientService;
import com.example.hospital.service.IRbService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
@Api(tags = "BasicController")
@RestController
@RequestMapping("/basic")
public class BasicController {
    @Autowired
    private IBasicService iBasicService;
    @Autowired
    private IPatientService iPatientService;
    @Autowired
    private IRbService iRbService;
    @ApiOperation(value = "获取基本信息")
    @GetMapping("/")
    public List<Basic> getBasic(){
        return iBasicService.list();
    }
    @ApiOperation(value = "分页获取基本信息")
    @GetMapping("/bypage")
    public ResPageBean getBasicByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                          @RequestParam(defaultValue = "10")Integer size,@RequestParam(defaultValue = "") String keyword){
        return iBasicService.getBasicByPage(currentPage,size,keyword);
    }

    @ApiOperation(value = "根据id获取基本信息")
    @GetMapping("/byid")
    public List<Basic> getBasicByid(@RequestParam Integer id){
        return iBasicService.list(new LambdaQueryWrapper<Basic>().eq(Basic::getId,id));
    }

    @ApiOperation(value = "添加基本信息")
    @PostMapping("/")
    public RespBean addBasic(@RequestBody Basic basic){
        if(iBasicService.save(basic)){
            return RespBean.success("添加成功");
        }else {
            return RespBean.error("添加失败");
        }
    }
    @ApiOperation(value = "修改基本信息")
    @PutMapping("/")
    public RespBean updateBasic(@RequestBody Basic basic){
        if(iBasicService.updateById(basic)){
            return RespBean.success("修改成功");
        }else {
            return RespBean.error("修改失败");
        }
    }
    @ApiOperation(value = "删除基本信息")
    @DeleteMapping("/{id}")
    public RespBean deleteBasic(@PathVariable Integer id){
        if(iBasicService.removeById(id)){
            return RespBean.success("删除成功");
        }else {
            return RespBean.error("删除失败");
        }
    }
    @ApiOperation(value = "获取病人所有基本信息")
    @GetMapping("/getBasic")
    public List<Patient> getBasic(@RequestParam Integer id){
        LambdaQueryWrapper<Rb> lam  = new LambdaQueryWrapper<>();
        //第一个时间
        LocalDate localDate = LocalDate.now();
        lam.eq(Rb::getPid,id);
        List<Rb> rbList = iRbService.list(lam);
        for (Rb rb : rbList){
            Integer id1 = rb.getId();
            //第二个时间
            LocalDate endDate = rb.getEndDate();
            if(localDate.isAfter(endDate)){
                iRbService.removeById(id1);
            }
        }
        return iPatientService.getBasic(id);
    }

}
